var blogEditor;
// Tags Input
$('#tags').tagsInput({
    width: '100%',
    height: '38px',
    defaultText: '文章标签'
});

var dir = $("#url").val();



$(function () {
    blogEditor = editormd("blog-editormd", {//编辑器加载
        width: "100%",
        height: 640,
        syncScrolling: "single",
        path: "/admin/plugins/editormd/lib/",
        toolbarModes: 'full',
        emoji:true,
        placeholder:"请输入博客内容",
        /**图片上传配置*/
        imageUpload: true,
        imageFormats: ["jpg", "jpeg", "gif", "png", "bmp", "webp"], //图片上传格式
        imageUploadURL: "/blogs/md/uploadfile?dir="+dir
        ,onload : function() {
            initPasteDragImg(this); //必须

        }
    });

    $("#save_btn").click(function () {
        if(valid_form()){
            layer.open({ //变成方法需要五个参数，一个返回值，中间也有不同的格式检验方法
                type: 1,
                title:'博客编辑',
                skin: 'layui-layer-rim', //加上边框
                area: ['666px', '450px'], //宽高
                content: $("#save_model").html(),
                shadeClose:true,
                tipsMore:true,
                btnAlign: 'c',
                btn: ['确认提交', '取消']
                ,btn1: function(index, layero){
                    var params1 = $("#blogForm").serialize();
                    var params2 = $("#save_form").serialize();
                    var params = params1+"&"+params2;
                    $.ajax({
                        type:"post",
                        url:'/blog/insert',
                        data:params,
                        success:function (r) {
                            if(r=='success'){
                                layer.open({
                                    icon:1,
                                    title:"success",
                                    content: "成功",
                                    shadeClose:true
                                });
                            }else{
                                layer.open({
                                    icon:2,
                                    title:"错误",
                                    content: r,
                                    shadeClose:true
                                });
                            }
                            setTimeout(function () { parent.layer.close(index) }, 100);//延迟
                            setTimeout(function () { window.location.href="/admin/blogs"}, 1500);
                        },
                        error: function (XMLHttpRequest, textStatus, errorThrown) {
                            console.log(XMLHttpRequest.status);
                            console.log(XMLHttpRequest.readyState);
                            console.log(textStatus);
                        }
                    })
                }
            });
            layui.use('form', function(){
                var form = layui.form;
                form.render();
            });
        }
    })

    //封面上传
    layui.use('upload', function(){
        var upload = layui.upload;
        var uploadInst = upload.render({
            elem: '#test1' //绑定元素
            ,url: '/blogImg/upload' //上传接口
            ,done: function(res){
                //上传完毕回调
                $("#img").val(res.img);
                $("#blogImg").attr("src","/images/blogs/header/"+res.img);
            }
            ,error: function(){
                //请求异常回调
                layer.msg("error");
            }
        });
    });


    /**
     * 随机封面功能
     */
    $("#randomImg").click(function () {
        var rand = parseInt(Math.random() * 40 + 1);
        $("#blogImg").attr("src", '/images/blogs/header/' + rand + ".jpg");
        $("#img").val(rand + ".jpg");
        /*$("#blogImg").attr("style", "width:60px1 ;height: 120px;display:block;");*/
    });

});





function valid_form() {
    var title = $("#title").val();
    var tags = $("#tags").val();
    var content = $("#content").text();
    var img = $("#img").val();
    if(isNull(title)){
        layer.msg("文章标题不能为空");
        return false;
    }
    if(isNull(tags)){
        layer.msg("文章标签不能为空");
        return false;
    }
    if(isNull(content)){
        layer.msg("文章内容不能为空");
        return false;
    }
    if(isNull(img)){
        layer.msg("请选择封面");
        return false;
    }
    return true;

}